/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.controller;

import com.xy.biz.base.client.EquipmentClient;
import com.xy.biz.base.domain.converter.EquipmentConverter;
import com.xy.biz.base.domain.query.EquipmentQuery;
import com.xy.biz.base.domain.req.GetEquipmentRequest;
import com.xy.biz.base.domain.req.ListDictItemRequest;
import com.xy.biz.base.domain.req.ListEquipmentRequest;
import com.xy.biz.base.domain.req.SaveEquipmentRequest;
import com.xy.biz.base.domain.resp.GetEquipmentResponse;
import com.xy.biz.base.domain.resp.ListDictItemResponse;
import com.xy.biz.base.service.DictService;
import com.xy.biz.base.service.EquipmentService;
import com.xy.lang.domain.KeyValue;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.enums.ApplyStatusEnum;
import com.xy.lang.enums.ScopeEnum;
import com.xy.lang.util.EnumUtils;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 *
 * @author yzh
 * @date 2021-4-13
 */
@Api(tags = "仪器设备服务")
@Slf4j
@RestController
@RequestMapping("/api/base/equipment")
public class EquipmentController implements EquipmentClient {

    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private DictService dictService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveEquipment(@RequestBody SaveEquipmentRequest request) {
        return equipmentService.saveEquipment(request);
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllEquipment(@RequestBody Collection<SaveEquipmentRequest> collection) {
        return equipmentService.saveAllEquipment(EquipmentConverter.toBO(collection));
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateEquipment(@RequestBody SaveEquipmentRequest request) {
        return equipmentService.updateEquipment(request);
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update_status")
    public Result<?> updateEquipmentStatus(@RequestBody SaveEquipmentRequest request) {
        return equipmentService.updateEquipmentStatus(request);
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteEquipment(@RequestBody GetEquipmentRequest request) {
        equipmentService.deleteEquipment(request.getEquipmentId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetEquipmentResponse> getEquipment(@RequestBody GetEquipmentRequest request) {
        return Result.success(EquipmentConverter.toResponse(equipmentService.getEquipment(request.getEquipmentId())));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetEquipmentResponse>> listEquipmentById(@RequestBody Collection<Long> collection) {
        return Result.success(EquipmentConverter.toResponse(equipmentService.listEquipmentById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetEquipmentResponse>> listEquipment(@RequestBody ListEquipmentRequest request) {
        return Result.success(EquipmentConverter.toResponse(equipmentService.listEquipment(EquipmentConverter.toQuery(request))));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetEquipmentResponse>> pageEquipment(@RequestBody ListEquipmentRequest request) {
        EquipmentQuery query = EquipmentConverter.toQuery(request);
        List<GetEquipmentResponse> list = EquipmentConverter.toResponse(equipmentService.listEquipment(query));

        int count = equipmentService.countEquipment(query);
        return Result.success(new Page<>(list, count));
    }

    @GetMapping(value = "/list_pre_options")
    public Result<Map<String, List>> listPreOptions() {
        Map<String, List> map = new HashMap<>();
        map.put("status", EnumUtils.toKeyValueList(ApplyStatusEnum.values(),
                (e) -> ((ApplyStatusEnum) e).getName()));
        map.put("scope", EnumUtils.toKeyValueList(ScopeEnum.values(),
                (e) -> ((ScopeEnum) e).getName()));
        List<ListDictItemResponse> dictList = dictService.listDictItem(
                ListDictItemRequest.builder().pageSize(1000).dictGroupCodes(
                        Arrays.asList("equipment_category")).build());
        if (!CollectionUtils.isEmpty(dictList)) {
            List<KeyValue<String, String>> equipmentCategoryList = new ArrayList<>();
            dictList.forEach(e -> {
                if ("equipment_category".equals(e.getDictGroupCode())) {
                    equipmentCategoryList.add(new KeyValue<>(e.getDictCode(), e.getDictValue()));
                }
            });
            map.put("equipmentCategory", equipmentCategoryList);
        }
        return Result.success(map);
    }

}
